/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author kien.tran
 */
public class RadioItem extends AListItem {

    private String group;
    private boolean checked = false;
    private int textWidth, paddingLeft = 0;

    public RadioItem(String id, String text) {
        super(text, null);
        super.id = id;
        this.checked = false;
        this.style.setPadding(new Padding(4));
        this.group = "";
    }

    public RadioItem(String id, String text, Image icon) {
        super(text, icon);
        super.id = id;
        this.checked = false;
        this.group = "";
    }

    public RadioItem(String id, String text, Image icon, String group) {
        super(text, icon);
        super.id = id;
        this.checked = false;
        this.group = group;
    }

    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }

    public void paint(int x, int y, Graphics g) {
        paint(x, y, g, false);
    }

    public void paint(int x, int y, Graphics g, boolean validate) {
        if (validate && !isPaint(x, y)) {
            return;
        }
        // pre-calc
        calculateHeight();
        setTextRows(text);

        // paint background
        g.setColor(focus == false ? style.getBgColor() : style.getSelectedBgColor());
        g.fillRect(x, y, width, height);

        // paint symbol
        paintUncheckSymbol(x + style.getPadding().getTop(), y + style.getPadding().getLeft(), g);
        if (checked) {
            paintCheckedSymbol(x + style.getPadding().getTop(), y + style.getPadding().getLeft(), g);
        }

        // paint text
        if (focus) {
            for (int i = 0; i < textRows.length; i++) {
                font.getFontNormal().drawString(g, textRows[i], x + style.getPadding().getLeft() + paddingLeft, y + style.getPadding().getTop() + i * (font.getFontNormal().getHeight() + style.getPadding().getTopAndBottom()), Graphics.TOP | Graphics.LEFT);
            }
        } else {
            font.getFontNormal().drawString(g, textRows[0], x + style.getPadding().getLeft() + paddingLeft, y + style.getPadding().getTop(), Graphics.TOP | Graphics.LEFT);
        }
    }

    public void setText(String text) {
        this.text = text;
        setTextRows(text);
    }

    protected void calculateHeight() {
        int textH = 0, iconH = 0;

        if (icon != null) {
            iconH = icon.getHeight();
        }

        if (textRows == null) {
            setTextRows(text);
        }

        if (focus) {
            textH = textRows.length * (font.getFontNormal().getHeight() + style.getPadding().getTopAndBottom());
        } else {
            textH = font.getFontNormal().getHeight() + style.getPadding().getTopAndBottom();
        }
        this.height = iconH > textH ? iconH : textH;
    }

    protected void setTextRows(String text) {
        paddingLeft = font.getFontNormal().getHeight() + style.getPadding().getLeft();
        textWidth = width - paddingLeft;

        if (text.length() > 0) {
//            textRows = normalFont.splitWith(text, textWidth);
            textRows = new String[]{text};
        } else {
            textRows = new String[]{""};
        }
    }

    private void paintCheckedSymbol(int x, int y, Graphics g) {
        int itemX = x + 3, itemY = y + 5, h = 0;
        g.setColor(0x393c39);
//        if (normalFont.getFontSize() == Font2.SIZE10N) {
//            h = 7;
//            g.fillRect(itemX, itemY, h, 3);
//            g.fillRect(itemX + 2, itemY - 2, 3, h);
//            g.fillRect(itemX + 1, itemY - 1, h - 2, h - 2);
//        } else if (normalFont.getFontSize() == Font2.SIZE12N) {
//            h = 9;
//            g.fillRect(itemX, itemY, h, 5);
//            g.fillRect(itemX + 2, itemY - 2, 5, h);
//            g.fillRect(itemX + 1, itemY - 1, h - 2, h - 2);
//        } else if (normalFont.getFontSize() == Font2.SIZE14N) {
//            h = 11;
//            g.fillRect(itemX, itemY, h, 7);
//            g.fillRect(itemX + 2, itemY - 2, 7, h);
//            g.fillRect(itemX + 1, itemY - 1, h - 2, h - 2);
//        }
         h = 9;
        g.fillRect(itemX, itemY, h, 5);
        g.fillRect(itemX + 2, itemY - 2, 5, h);
        g.fillRect(itemX + 1, itemY - 1, h - 2, h - 2);
    }

    private void paintUncheckSymbol(int x, int y, Graphics g) {
        g.setColor(0x4a4d4a);
//        if (normalFont.getFontSize() == Font2.SIZE10N) {
//            g.drawArc(x, y, 12, 12, 0, 360);
//        } else if (normalFont.getFontSize() == Font2.SIZE12N) {
//            g.drawArc(x, y, 14, 14, 0, 360);
//        } else if (normalFont.getFontSize() == Font2.SIZE14N) {
//            g.drawArc(x, y, 16, 16, 0, 360);
//        }
        g.drawArc(x, y, 14, 14, 0, 360);
    }
}
